package com.example.qxfw.shfx.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.qxfw.common.entity.qx.SmallScaleStation;
import com.example.qxfw.common.util.DateUtil;
import com.example.qxfw.rcyw.mapper.SmallScaleStationMapper;
import com.example.qxfw.shfx.Service.YqService;
import com.example.qxfw.shfx.dto.YqResult;
import com.example.qxfw.ybzz.mapper.MsgMediumSmallScaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class YqServiceImpl implements YqService {
    @Autowired
    private MsgMediumSmallScaleMapper msgMediumSmallScaleMapper;
    @Autowired
    private SmallScaleStationMapper smallScaleStationMapper;
    @Override
    public List<YqResult> searchByTime(int choose, String startTime, String endTime, String stationName, String type, float rain) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        Timestamp newTime = Timestamp.valueOf(now);

        // 1小时前时间
        LocalDateTime oneHourBefore = now.minusHours(1);
        Timestamp oneHourBeforeTimestamp = Timestamp.valueOf(oneHourBefore);

        // 3小时前时间
        LocalDateTime threeHoursBefore = now.minusHours(3);
        Timestamp threeHoursBeforeTimestamp = Timestamp.valueOf(threeHoursBefore);

        // 1天前时间
        LocalDateTime oneDayBefore = now.minusDays(1);
        Timestamp oneDayBeforeTimestamp = Timestamp.valueOf(oneDayBefore);

        // 3天前时间
        LocalDateTime threeDaysBefore = now.minusDays(3);
        Timestamp threeDaysBeforeTimestamp = Timestamp.valueOf(threeDaysBefore);

        // 7天前时间
        LocalDateTime sevenDaysBefore = now.minusDays(7);
        Timestamp sevenDaysBeforeTimestamp = Timestamp.valueOf(sevenDaysBefore);

        // 自定义时间（如果有一个参数为空，就只查询当天的）
        Timestamp start = null;
        if (startTime != null || !startTime.isEmpty()) {
            start = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(startTime));
        }
        Timestamp end = null;
        if (endTime != null || !endTime.isEmpty()) {
            end = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(endTime));
        }

        List<YqResult> list;
        switch (choose) {
            case 1:
                list = msgMediumSmallScaleMapper.searchByTime(newTime, oneHourBeforeTimestamp, stationName, type, rain);
                break;
            case 2:
                list = msgMediumSmallScaleMapper.searchByTime(newTime, threeHoursBeforeTimestamp, stationName, type, rain);
                break;
            case 3:
                list = msgMediumSmallScaleMapper.searchByTime(newTime, oneDayBeforeTimestamp, stationName, type, rain);
                break;
            case 4:
                list = msgMediumSmallScaleMapper.searchByTime(newTime, threeDaysBeforeTimestamp, stationName, type, rain);
                break;
            case 5:
                list = msgMediumSmallScaleMapper.searchByTime(newTime, sevenDaysBeforeTimestamp, stationName, type, rain);
                break;
            case 6:
                list = msgMediumSmallScaleMapper.searchByTime(start, end, stationName, type, rain);
                break;
            default:
                list = new ArrayList<>(); // 默认情况下返回空列表
                break;
        }
        return list;
    }

    @Override
    public List<String> getAllStationName() {
        List<SmallScaleStation> smallScaleStations = smallScaleStationMapper.selectList(new LambdaQueryWrapper<SmallScaleStation>()
                .select(SmallScaleStation::getStationname));
        List<String> distinctStationNames = smallScaleStations.stream()
                .map(station -> Optional.ofNullable(station.getStationname()).orElse(""))
                .distinct()
                .collect(Collectors.toList());
        return distinctStationNames;

    }
}
